<?php

declare (strict_types=1);

namespace quick\admin\library;


use think\Container;
use think\Validate;

/**
 *  表单验证器
 *
 * Class FormValidate
 * @package quick\library
 */
class FormValidate extends Validate
{

    /**
     *
     * @var array
     */
    public $ruleNameAlias = [
        'number' => "number",
        'boolean' => "boolean",
        'bool' => "boolean",
        'integer' => "integer",
        'float' => "float",
        'array' => "array",
        'date' => "date",
        'email' => "email",
        'url' => "url",
    ];

    public function getRuleMsg(string $attribute, string $title, string $type, $rule)
    {
        return parent::getRuleMsg($attribute, $title, $type, $rule);
    }

    public function parseErrorMsg(string $msg, $rule, string $title)
    {
        return parent::parseErrorMsg($msg, $rule, $title); // TODO: Change the autogenerated stub
    }

    public function getypeMsg($key)
    {
        return $this->typeMsg[$key] ?? '';
    }

    public function getRegex($key)
    {
        return $this->defaultRegex[$key] ?? '';
    }

    public function getErrorMsgByRule($key, $rule, $title)
    {
        $msg = $this->parseErrorMsg($this->getypeMsg($key), $rule, $title);
        return $msg ?? '';
    }


    public function transform($rule, $title, $valueType = 'string')
    {
        $vueRules = [];
        $trigger = 'change';
//        $trigger = 'blur';
        if (is_string($rule)) {
            $ruleArr = explode(":", $rule);
            $type = $ruleArr[0];
            if ($type == "require") {
                // todo 数据类型根据实际情况变得
                $vueRules = ['type' => $valueType, "required" => true, 'trigger' => $trigger, "message" => $this->getErrorMsgByRule("require", '', $title)];
            } elseif (isset($this->ruleNameAlias[$type])) {

                $vueRules = ["type" => $this->ruleNameAlias[$type], 'trigger' => $trigger, "message" => $this->getErrorMsgByRule($type, '', $title)];

            } elseif (!empty($this->getRegex($type))) {

                $vueRules = ['pattern' => $this->getRegex($type), 'trigger' => $trigger, "message" => $this->getErrorMsgByRule($type, '', $title)];

            } elseif ($type == "in") {

                $vueRules = ["type" => "enum", "enum" => explode(",", $ruleArr[1]), 'trigger' => $trigger, "message" => $this->getErrorMsgByRule($type, $ruleArr[1], $title)];

            } elseif (in_array($type, ["min", "max"])) {

                $vueRules = [$type => (int)$ruleArr[1], 'type' => $valueType, 'trigger' => $trigger, "message" => $this->getErrorMsgByRule($type, $ruleArr[1], $title)];

            } elseif ($type == "between") {

                [$min, $max] = explode(",", $ruleArr[1]);
                $vueRules = ['type' => $valueType, "min" => (int)$min, "max" => (int)$max, 'trigger' => $trigger, "message" => $this->getErrorMsgByRule($type, $ruleArr[1], $title)];

            }

        }
        return $vueRules;
    }


    /**
     * 静态实例对象
     * @param array $args
     * @return static
     */
    public static function instance(...$args)
    {
        return Container::getInstance()->make(static::class, $args);
    }

}
